// pages/print/papers/papers.js
var utils = require("../../../utils/utils.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    adv:[],
    list: [],
    firstOpen:0
  },
  getIndex(code) {
    var that = this;
    utils.request('serviceItem/serviceItemLists', {}, function (res) {
      if (res.data.code == 200) {
        var docArray = [];
        for(var i=0;i<res.data.data.length;i++){
            if('idPhoto'==res.data.data[i].config.tag){
                docArray = res.data.data[i].child
            }
        }
        var array = [];
        for(var i=0;i<docArray.length;i++){
            if(docArray[i].status!=-1){
                array.push(docArray[i])
            }
        }
        that.setData({
          list:array
        })
      } else {
        utils.toast(res.data.msg)
      }
    })
  },
  goSearchZjz(){
      var that = this;
      wx.navigateTo({
        url: '/pages/print/papersSearch/papersSearch?tagFloor='+that.data.tcode+'&serviceMainId='+that.data.serviceMainId,
      })
  },
  changeIndex(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var code = e.currentTarget.dataset.code;
    var status = e.currentTarget.dataset.status;
    var name = e.currentTarget.dataset.name
    var index = e.currentTarget.dataset.index;
    var detail = that.data.list[index].remarks;
    var pic =  e.currentTarget.dataset.pic;
    if (status == '-1') {
      utils.toast('当前打印机不支持此方式');
      return false;
    }
    wx.navigateTo({
      url: '../specification/specification?name='+name+'&tagFloor='+that.data.tcode+'&pic='+pic+'&detail='+JSON.stringify(detail)+'&serviceMainId='+that.data.serviceMainId+'&serviceSubtypeId='+id+'&code='+code,
    })
  },
  getBanner(){
    var that = this;
    utils.request('ad/lists/91684762', {}, function (res) {
        that.setData({
            adv:res.data.data
        })
    })
  },
  goToLink(e) {
    var url = e.currentTarget.dataset.link;
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    var content = e.currentTarget.dataset.content;
    if (utils.is_define(url)) {
        if (url.indexOf('pages') != -1 && url.indexOf('mjyy') == -1) {
            wx.navigateTo({
                url: url,
            })
            return false;
        }
        if (url.indexOf('pages') != -1 && url.indexOf('mjyy') != -1) {
            wx.navigateToMiniProgram({
                appId: 'wxd8a7d886dc3c37d5',
                path: url,
                envVersion: 'release',
                success(res) {
                    // 打开成功
                }
            })
            return false;
        }
        if (url.indexOf('https') != -1) {
            wx.navigateTo({
                url: '/pages/my/agreement/agreement?url=' + url
            })
            return false;
        }
        if(content){
            //信息详情
            wx.navigateTo({
              url: '/pages/my/pictureAdv/pictureAdv?id='+id+'&name='+title
            })
        }
    }
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
          tcode:options.code,
          serviceMainId:options.serviceMainId
      })
    this.getBanner();
    this.getIndex(options.code);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.data.firstOpen == 1) {
      this.getIndex(that.data.tcode)
    } else {
      that.setData({
        firstOpen: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})